package cwb1;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.EntityNotFoundException;

import java.io.IOException;



public class registerServlet extends HttpServlet
{
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
			{
				String id = req.getParameter("username");
				String password = req.getParameter("password");
				String usernumber = req.getParameter("usernumber");
				String boodschap = null;
				System.out.println(req.getParameter("selectedCourses"));
				boolean test = UserRegistry.getInstance().verifyRegistration(id);
				if(test==true)
				{
					User user = new User(id, usernumber, 1, password);
					UserRegistry userRegistry = UserRegistry.getInstance();
					userRegistry.add(user);
					boodschap = "You've been successfully registered.";
					
					for(Division course : DivisionRegistry.getInstance().getAllType(DivisionFactory.COURSE)) {
						String courseId = req.getParameter(course.getId());
						if(courseId != null) {
							course.addUserToCourse(user);
							try  {
								DivisionRegistry.getInstance().update(course);
							}
							catch (EntityNotFoundException e) {
								;
							}
						}
					}
					

				}
				
				else
				{
					boodschap = "This username is already in use.";
				}	
				resp.sendRedirect("login.jsp?boodschap=" + boodschap);
				
			}
	
	private void newUser(String id, String password, String usernumber)
	{
		
		
	}
	
}